//  User.js
const mongoose = require("mongoose");
const transDatePlugin = require("../../plugins/transDatePlugin");
const IdsPlugin = require("../../plugins/IdsPlugin");
const Schema = mongoose.Schema;

const UserSchema = new Schema(
	{
		id: {
			type: Number,
			required: true, //是否必需
			unique: true, //字段唯一性
			default: 1,
		},
		user_name: {
			// 用户名
			type: String,
			required: true, //是否必需
			minlength: 5,
			maxlength: 20,
		},
		password: {
			// 用户密码
			type: String,
			required: true, //是否必需
		},
		nick_name: {
			// 用户昵称
			type: String,
		},
		avatar: {
			// 用户头像
			type: String,
		},
		mobile: {
			type: Number,
		},
		is_delete: {
			type: Boolean,
			default: false,
		},
		lock_expire: {
			// 用户锁定时间
			type: String,
		},
		error_password_times: {
			// 密码错误次数
			type: Number,
			default: 0,
		},
		create_time: {
			type: String,
			required: true,
			default: Date.now,
		},
		update_time: {
			type: String,
		},
		status: {
			type: String,
			default: "1",
		},
	},
	{
		versionKey: false,
		timestamps: { createdAt: "create_time", updatedAt: "update_time" },
	}
);

UserSchema.plugin(transDatePlugin, { paths: ["update_time"] });
UserSchema.plugin(IdsPlugin, "user");
module.exports = User = mongoose.model(
	"user",
	UserSchema,
	`${process.env.PREFIX}_user`
);
